<?php
/**
 * User: 乃火
 * Time: 2023/9/13 3:38 下午
 * QQ: 1123416584
 */

namespace addons\screen\controller;


use app\admin\library\Auth;
use app\common\controller\Api;

class Base extends Api
{

    protected $noNeedLogin = ['*'];

    // login/share 满足一个即可
    protected $needLogin = ['*'];
    protected $needShare = [];
    protected $share = null;

    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub

        $config = get_addon_config('screen');


        $is = false;
        $need = false;
        if ($this->auth->match($this->needLogin)) {
            $need = true;
            if ((new Auth())->isLogin()) {
                $is = true;
            }

            // 判断是否开放编辑
            if ($config['open_edit']) {
                $is = true;
            }
        }

        if (false == $is && $this->auth->match($this->needShare)) {
            $need = true;
            $token = $this->request->header('Share-Token');
            $share = \app\common\model\screen\Share::where('share_token', trim($token, ','))->find();
            if ($share && $share['status'] == 'normal' && $share['end_time'] > time()) {
                $is = true;
            }
            $this->share = $share;

            // 判断是否全部开放预览
            if ($config['open_preview']) {
                $this->share = true;
                $is = true;
            }
        }

        if (false == $is && $need) {
            $this->error('无权限操作');
        }
    }


    protected function success(
        $msg = '',
        $data = null,
        $code = 200,
        $type = null,
        array $header = []
    ) {
        parent::success($msg, $data, $code, $type,
            $header);
    }
}